<template>
  <el-card class="main">
    <el-form
      ref="ruleFormRef"
      :model="form"
      label-width="auto"
      :rules="rules"
      style="max-width: 600px"
    >
      <el-form-item label="姓名:">
        <el-input v-model="form.nickname" disabled />
      </el-form-item>
      <el-row>
        <el-col :span="11">
          <el-form-item label="电话:">
            <el-input v-model="form.phone" disabled />
          </el-form-item>
        </el-col>
        <el-col :span="2"></el-col>
        <el-col :span="11">
          <el-form-item label="角色:">
            <el-input v-model="form.position" disabled />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="11">
          <el-form-item label="部门:">
            <el-input v-model="form.department" disabled />
          </el-form-item>
        </el-col>
        <el-col :span="2"></el-col>
        <el-col :span="11">
          <el-form-item label="职务:">
            <el-input v-model="form.title" disabled />
          </el-form-item>
        </el-col>
      </el-row>
      <el-form-item label="新的密码" prop="newPassword">
        <el-input v-model="form.newPassword" type="password" clearable></el-input>
      </el-form-item>
      <el-form-item label="重复密码" prop="confirmPassword">
        <el-input v-model="form.confirmPassword" type="password" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm(ruleFormRef)">修改密码</el-button>
      </el-form-item>
    </el-form>
  </el-card>
</template>

<script lang="ts" setup>
  import { reactive, ref } from 'vue'
  import { ElMessage } from 'element-plus'
  import { useUserStore } from '@/store/modules/user'
  import { putSysUserPassword } from '@/api/controller/sys-user-controller/index'
  const UserStore = useUserStore()
  const form = reactive({
    nickname: UserStore.userInfo.nickname,
    phone: UserStore.userInfo.phone,
    department: UserStore.userInfo.department,
    title: UserStore.userInfo.title,
    position: UserStore.userInfo.position,
    newPassword: '',
    confirmPassword: '',
  })
  const rules = reactive({
    confirmPassword: [
      {
        required: true,
        message: '请重复输入新的密码',
        trigger: 'blur',
      },
      { min: 0, max: 64, message: '长度不超过64个字符', trigger: 'blur' },
    ],
    newPassword: [
      {
        required: true,
        message: '请输入新的密码',
        trigger: 'blur',
      },
      { min: 0, max: 64, message: '长度不超过64个字符', trigger: 'blur' },
    ],
  })
  import type { ElForm } from 'element-plus'
  type FormInstance = InstanceType<typeof ElForm>
  const ruleFormRef = ref<FormInstance>()
  const submitForm = (formEl: FormInstance | undefined) => {
    if (!formEl) return
    formEl.validate(async (valid) => {
      if (valid) {
        const res = await putSysUserPassword(form)
        if (res.code === 200) {
          ElMessage.success('修改成功')
          resetForm(formEl)
          UserStore.needChange = false
        } else {
          resetForm(formEl)
        }
      } else {
        ElMessage.warning('输入有误')
        resetForm(formEl)
        return false
      }
    })
  }
  const resetForm = (formEl: FormInstance | undefined) => {
    if (!formEl) return
    formEl.resetFields()
  }
</script>

<style lang="scss" scoped>
  .main {
    margin: 20px;
  }
</style>
